#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>


#include "proto.h"

#define IPSIZE 20


int main(){
	int sd,ret;
	char buf[IPSIZE];
	sd=socket(AF_INET,SOCK_DGRAM,0);
	if(sd<0){
		perror("socket");
		exit(1);
	}
	struct sockaddr_in laddr,raddr;
	laddr.sin_family=AF_INET;
	laddr.sin_port=htons(NETPORT);
	socklen_t raddr_len;
	ret=inet_pton(AF_INET,"0.0.0.0",&laddr.sin_addr);
	if(ret<=0){
		perror("inet_pton");
		exit(1);
	}

	ret=bind(sd,(void *)&laddr,sizeof(laddr));
	if(ret<0){
		perror("bind");
		exit(1);
	}
	struct msg_st msg;
	raddr_len=sizeof(raddr);
	while(1){
		ret=recvfrom(sd,&msg,sizeof(msg),0,(void *)&raddr,&raddr_len);
		if(ret<0){
			perror("recvfrom");
			exit(1);
		}
		printf("name:%s\nchinese:%d\nmath%d\n",msg.name,ntohl(msg.chinese),ntohl(msg.math));
		inet_ntop(AF_INET,&raddr.sin_addr,buf,IPSIZE);
		printf("raddr ip:%s\nport :%d\n",buf,ntohs(raddr.sin_port));
	}
	close(sd);


}


